﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using rFDevLib;

namespace rF1EngineConversion {
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window {
        private TorqueCurveGraphic gfx = new TorqueCurveGraphic();
        rF1Engine engine = new rF1Engine();
        rF2Engine rf2Eng = new rF2Engine();

        public MainWindow() {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
            InitializeComponent();

            UpdateTextDisplay();
        }

        private void In_OnChange( object sender, EventArgs e ) {
            UpdaterF2Engine( null, null );
        }

        private void UpdateTextDisplay() {
            LblPerf.Content = "RF2 - " + rf2Eng.GetPowerInfo() + " - RF1 - " + engine.GetPowerInfo();
            LblFileName.Content = engine.fileName;
        }

        private void UpdaterF2Engine( object sender, RoutedEventArgs e ) {
            if ( engine.m_params.Count == 0 )
                return;

            rf2Eng.stoich = InStoich.Value;
            rf2Eng.fuelDensity = InFuelDensity.Value/1000.0;
            rf2Eng.chokeRPM = InChokeRPM.Value;
            rf2Eng.preserveTorqueCurve = CbPreserveTorque.IsChecked.GetValueOrDefault( true );

            rf2Eng.BuildTorqueCurve( engine );

            rf2Eng.DrawGraphic( gfx );
            ImgTorqueCurve.Source = gfx.GetBmpSrc();

            UpdateTextDisplay();
        }

        private void BtnOpen_Click( object sender, RoutedEventArgs e ) {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.FileName = string.Empty;
            dlg.DefaultExt = ".ini";
            dlg.Filter = "rFactor 1 engine (.ini)|*.ini|GTR2/R07 engine (.eng)|*.eng";

            Nullable<bool> result = dlg.ShowDialog();

            if ( result != true ) return;

            try {
                engine.OpenFile( dlg.FileName );
                UpdaterF2Engine( null, null );
            } catch ( Exception exc ) {
                MessageBox.Show( "Uh oh, there's been a problem! Are you sure this is a valid rF1 engine? If so, send a screenshot of this dialog to the developer!\n\n"+exc.ToString(), "Unable to open this engine!", MessageBoxButton.OK, MessageBoxImage.Warning );
            }

            InChokeRPM.Value = engine.GetMaxRPM();
        }

        private void BtnSaveAs_Click( object sender, RoutedEventArgs e ) {
            Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
            dlg.FileName = engine.fileName;
            dlg.DefaultExt = ".ini";
            dlg.Filter = "rFactor 2 engine (.ini)|*.ini";

            Nullable<bool> result = dlg.ShowDialog();

            if ( result != true ) return;

            System.IO.StreamWriter w = new System.IO.StreamWriter( dlg.FileName );
            rf2Eng.WriteToStream( w );
            w.Close();
        }

        private void Hyperlink_RequestNavigate_1( object sender, RequestNavigateEventArgs e ) {
            Process.Start( new ProcessStartInfo( e.Uri.AbsoluteUri ) );
            e.Handled = true;
        }
    }
}
